package com.weibo;

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Robot;
import java.awt.event.InputEvent;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import chrriis.common.UIUtils;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserAdapter;

public class SinaWeibo {

	public static final String LS = System.getProperty("line.separator");

	private static final String LOGIN_URL = "http://weibo.com/";
	
	public static Robot REBOT = null;

	public static JWebBrowser WEB_BROWSER = new JWebBrowser();

	private String userName;
	private String pwd;

	public SinaWeibo(String userName, String pwd) {
		this.userName = userName;
		this.pwd = pwd;

		try {
			REBOT = new Robot();
		} catch (AWTException e1) {
			e1.printStackTrace();
			System.exit(0);
		}

	}

	private JComponent createContent() {

		JPanel contentPane = new JPanel(new BorderLayout());
		JPanel webBrowserPanel = new JPanel(new BorderLayout());
		webBrowserPanel.setBorder(BorderFactory.createTitledBorder("新浪微博自动登录"));
		contentPane.add(webBrowserPanel, BorderLayout.CENTER);
		
		SinaWeibo.WEB_BROWSER.setButtonBarVisible(true);
		SinaWeibo.WEB_BROWSER.setMenuBarVisible(true);
		SinaWeibo.WEB_BROWSER.setBarsVisible(true);
		SinaWeibo.WEB_BROWSER.setStatusBarVisible(true);

		SinaWeibo.WEB_BROWSER.navigate( SinaWeibo.LOGIN_URL );

		JWebBrowser.clearSessionCookies();
		
		webBrowserPanel.add( SinaWeibo.WEB_BROWSER, BorderLayout.CENTER );
		
		//登录监听器
		WeiBoLoginListener loginListen = new WeiBoLoginListener(  this.userName , this.pwd );		
		SinaWeibo.WEB_BROWSER.addWebBrowserListener( loginListen );
		
		WeiBoCommentListener commentListener = new WeiBoCommentListener();
		SinaWeibo.WEB_BROWSER.addWebBrowserListener( commentListener );
				
		return contentPane;
		
	}
	
	public void open() {
		UIUtils.setPreferredLookAndFeel();
		NativeInterface.open();
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JFrame frame = new JFrame("新浪微博");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.getContentPane().add( createContent(), BorderLayout.CENTER );

				frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
				frame.setLocationByPlatform(true);

				frame.setLocationByPlatform(true);
				frame.setVisible(true);
			}
		});
		NativeInterface.runEventPump();
	}
	
	/**
	 * 删除监听器
	 * @param listener
	 */
	public static void removeListener( WebBrowserAdapter listener ){
		SinaWeibo.WEB_BROWSER.removeWebBrowserListener( listener );
	}
	
	/**
	 * 导航
	 * @param listener
	 */
	public static void navigate( String url ){
		SinaWeibo.WEB_BROWSER.navigate( url );
	}
	
	public static void close(){
		
		//开始评论
		SinaWeibo.REBOT.mouseMove(675,509); // 移动
		SinaWeibo.REBOT.mousePress(InputEvent.BUTTON1_MASK); // 按下鼠标左键
		SinaWeibo.REBOT.mouseRelease(InputEvent.BUTTON1_MASK); // 释放鼠标左键
		
	}
	
	public static void main( String[] args ) {

		SinaWeibo weibo = new SinaWeibo( "wcj_zxq@126.com","wcj19830417" );
		weibo.open();
		
	}

}
